package nl.stroep.classbuilder.cb
{
	import nl.stroep.classbuilder.cb.interfaces.ICbObject;
	import nl.stroep.classbuilder.definitions.Accessor;
	import nl.stroep.classbuilder.definitions.Chars;
	import nl.stroep.classbuilder.definitions.ClassMethods;
	import nl.stroep.classbuilder.definitions.DataTypes;
	import nl.stroep.classbuilder.templates.Templates;
	import nl.stroep.classbuilder.templates.TemplateVariableDefinitions;
	import nl.stroep.classbuilder.utils.StringUtil;
	
	/**
	 * Object definition
	 * @author Mark Knol - Stroep.nl - 2010 (c)
	 */
	[bindable]
	public final class CbObject implements ICbObject
	{
		private var _name:String;
		private var _type:String;
		private var _accessModifier:String;
		private var _modifier:String;
		private var _defaultValue:String;
		private var _isGetter:Boolean;
		private var _isSetter:Boolean;
		
		public var classMethod:String;
		public var parameters:Vector.<CbParameter>;
		
		public function CbObject(classMethod:String, name:String, type:String = null, defaultValue:String = null, accesModifier:String = "public", modifier:String = null, isGetter:Boolean = false, isSetter:Boolean = false ):void
		{ 
			this._name = name;
			this._type = type;
			this._defaultValue = defaultValue;
			this._accessModifier = accesModifier;
			this._modifier = modifier;
			this._isGetter = isGetter;
			this._isSetter = isSetter;
			
			this.classMethod = classMethod;
		}
		
		private function getParameters():String
		{
			var retval:String = Chars.EMPTY;
			
			if (parameters || type == ClassMethods.FUNCTION)
			{
				retval += Chars.LEFT_PARENTHESIS;
				
				const TOTAL:int = parameters.length;
				
				for (var i:uint = 0; i < parameters.length; ++i) 
				{
					var parameter:CbParameter = parameters[i];
					retval += parameter.toString();
					retval += ( i != TOTAL - 1 )? Chars.COMMA : Chars.EMPTY;
				}
				
				retval += Chars.RIGHT_PARENTHESIS;
			}
			return retval;
		}
		
		public function toString( ):String 
		{
			var retval:String = Templates.getInstance().objectTemplate;
			
			retval = StringUtil.replace(retval, TemplateVariableDefinitions.ACCESS_MODIFIER, this._accessModifier);
			retval = StringUtil.replace(retval, TemplateVariableDefinitions.MODIFIER, this._modifier ? this._modifier : Chars.EMPTY);
			retval = StringUtil.replace(retval, TemplateVariableDefinitions.GETSET, this._isGetter ? Accessor.GET : (this._isSetter ? Accessor.SET : Chars.EMPTY));
			retval = StringUtil.replace(retval, TemplateVariableDefinitions.NAME, this._name);
			retval = StringUtil.replace(retval, TemplateVariableDefinitions.RETURNTYPE, this._type ? this._type : DataTypes.VOID);
			retval = StringUtil.replace(retval, TemplateVariableDefinitions.PARAMETERS, getParameters());
			retval = StringUtil.replace(retval, TemplateVariableDefinitions.CLASS_METHOD, classMethod);
			retval = StringUtil.replace(retval, TemplateVariableDefinitions.DEFAULT_VALUE, (!defaultValue) ? Chars.EMPTY : this._defaultValue);
			
			return retval;
		}
		
		public function get name():String { return _name; }
		
		public function set name(value:String):void 
		{
			_name = value;
		}
		
		public function get type():String { return _type; }
		
		public function set type(value:String):void 
		{
			_type = value;
		}
		
		public function get accessModifier():String { return _accessModifier; }
		
		public function set accessModifier(value:String):void 
		{
			_accessModifier = value;
		}
		
		public function get modifier():String { return _modifier; }
		
		public function set modifier(value:String):void 
		{
			_modifier = value;
		}
		
		public function get defaultValue():String { return _defaultValue; }
		
		public function set defaultValue(value:String):void 
		{
			_defaultValue = value;
		}
		
		public function get isGetter():Boolean { return _isGetter; }
		
		public function set isGetter(value:Boolean):void 
		{
			_isGetter = value;
		}
		
		public function get isSetter():Boolean { return _isSetter; }
		
		public function set isSetter(value:Boolean):void 
		{
			_isSetter = value;
		}
		
	}
}